package cn.fl.mcr.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.mcr.service.IMcrBillDService;
import cn.fl.mcr.vo.McrBillDVO;
import cn.fl.system.constant.SysLogConstant;
import cn.lionbridgecapital.loan.model.condition.McrBillDQC;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @Description: 小贷账单明细表 控制层
 */
@Controller
@RequestMapping("/mcr/mcrBillD")
public class McrBillDController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(McrBillDController.class);

    /**
     * McrBillD 服务层BEAN
     */
    @Autowired
    private IMcrBillDService mcrBillDService;

    /**
     * 到列表页面
     */
    @RequestMapping(value = "/toListPage.do", method = {RequestMethod.GET})
    public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toListPage", "加载McrBillD列表页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("mcr/mcrBillD/mcrBillDList");
        return mav;
    }

    /**
     * 初始化列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/initList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initMcrBillDList(HttpServletRequest request, HttpServletResponse response) {
        McrBillDQC qc = new McrBillDQC();
        //设置查询条件？
        String billId = request.getParameter("billId");
        if (StringUtils.isNotEmpty(billId)) {
            qc.setBillId(Long.valueOf(billId));
        }
        qc.setIsDel(BaseConstant.IS_YESNO_NO);
        qc.setSortName("PAY_PD");
        BaseLogUtils.info(logger, "initMcrBillDList", "初始化McrBillD列表页面数据.DATA:" + JSONObject.toJSONString(qc), CurrentThreadContext.getCurrentUserName());
        List<McrBillDVO> list = this.mcrBillDService.searchList(qc);
        ListPageVO<McrBillDVO> listPage = null;
        if(CollectionUtils.isNotEmpty(list)){
            listPage = new ListPageVO<McrBillDVO>(list, list.size());
        }
        return listPage;
    }

    /**
     * 得到一条记录
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getById.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object getMcrBillDById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        String id = request.getParameter("id");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
        if (StringUtils.isEmpty(id)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
            BaseLogUtils.info(logger, "getMcrBillDById", "获取McrBillD数据,主键参数为空.", CurrentThreadContext.getCurrentUserName());
            return result;
        }

        try {
            McrBillDVO vo = this.mcrBillDService.selectById(Long.valueOf(id));
            if (null == vo) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
                BaseLogUtils.info(logger, "getMcrBillDById", "获取McrBillD数据不存在,ID:" + id, CurrentThreadContext.getCurrentUserName());
            }
            result.setData(vo);
            BaseLogUtils.info(logger, "getMcrBillDById", "获取McrBillD数据成功,ID:" + id, CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.info(logger, "getMcrBillDById", "获取McrBillD数据失败,ID:" + id + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.error(logger, "getMcrBillDById", "获取McrBillD数据失败,系统错误.ID:" + id + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    /**
     * 获取请求参数
     *
     * @param request 请求
     * @return
     */
    private McrBillDVO getRequestParamForMcrBillD(HttpServletRequest request) throws BaseException {
        //获取参数
        String id = request.getParameter("id");//主键
        String billId = request.getParameter("billId");//账单ID
        String payPd = request.getParameter("payPd");//期次
        String payDt = request.getParameter("payDt");//还款时间
        String capAmt = request.getParameter("capAmt");//应还本金
        String intrAmt = request.getParameter("intrAmt");//应还利息
        String paidCapAmt = request.getParameter("paidCapAmt");//已还本金
        String paidIntrAmt = request.getParameter("paidIntrAmt");//已还利息
        String version = request.getParameter("version");//版本号

        //验证参数并赋值
        McrBillDVO entity = new McrBillDVO();
        //entity.setBillId(billId);//账单ID
        //entity.setPayPd(payPd);//期次
        //entity.setPayDt(payDt);//还款时间
        //entity.setCapAmt(capAmt);//应还本金
        //entity.setIntrAmt(intrAmt);//应还利息
        //entity.setPaidCapAmt(paidCapAmt);//已还本金
        //entity.setPaidIntrAmt(paidIntrAmt);//已还利息
        //entity.setVersion(version);//版本号

//		if (StringUtils.isBlank(name)) {
//            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "名称不能为空.");
//      }
//      if (StringUtils.isNotBlank(num)) {
//          if (!StringUtils.isUnsignedNumeric(num)) {
//                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "请填写正确的数字.");
//          }
//      }

        Date tm = new Date();
        if (StringUtils.isNotBlank(id)) {
            entity.setId(Long.valueOf(id));
            entity.setMdfTm(tm);
            entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
        } else {
            entity.setCrtTm(tm);
            entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
            //entity.setCrtUsrOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前用户组织机构代码
            //entity.setOrgCd(CurrentThreadContext.getCurrentBuOrgCd());//事业部组织机构代码
        }

        return entity;
    }
}

